#include "data_flood.h"
#include <getopt.h>

#define DF_VERSION "1.0.0"

UserArgument user_arg;

static struct option long_options[] = {
	{"help", no_argument, NULL, '?'},
	{"version", no_argument, NULL, 'V'},
	{"user", required_argument, NULL, 'U'},
	{"port", required_argument, NULL, 'p'},
    {"password", required_argument, NULL, 'P'},
	{"database", required_argument, NULL, 'd'},
	{"force", no_argument, NULL, 'f'},
	{"configure", required_argument, NULL, 'c'},
	{"relation", required_argument, NULL, 'r'},
    {"parallel", required_argument, NULL, 'j'},
    {"files", required_argument, NULL, 'n'},
    {"unique", required_argument, NULL, 'u'},
    {"mode", required_argument, NULL, 'm'},
	{NULL, 0, NULL, 0}
};

static void df_logo(void)
{
    df_log_0("###############################################################");
    /*df_log_0("---D D D D ------------A---------------------------------------");
    df_log_0("---D-------D----------A-A--------------------------------------");
    df_log_0("---D--------D--------A---A-------------------------------------");
    df_log_0("---D--------D-------A A A A------------------------------------");
    df_log_0("---D-------D-------A-------A-----------------------------------");
    df_log_0("---D D D D -------A---------A----------------------------------");
    df_log_0("---------------------------------------------------------------");*/
    df_log_0("+++++++++++Contact Author By mail(lchch1990@sina.cn)+++++++++++");
    //df_log_0("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
    df_log_0("###############################################################");
}

static void do_version(void)
{
    df_log_0("Data Flood Version %s", DF_VERSION);
}

static void do_help(void)
{
    df_log_0("#####################################################");
    df_word();
    df_log_0("#####################################################");
    df_log_0("data_flood options");
    df_log_0("-h show this help");
    df_log_0("-V show data_flood version");
    df_log_0("-v show debug info");
    df_log_0("-p port to connect databse");
    df_log_0("-P password to connect databse");
    df_log_0("-U user to connect databse");
    df_log_0("-d database name to connect");
    df_log_0("-u unique value column");
    df_log_0("-r relation to expend");
    df_log_0("-j parallel dop for data generate");
    df_log_0("-n relation size(by GB)");
    df_log_0("-f truncate the table if exists");
    df_log_0("#####################################################");
    df_exit(NULL);
}

void df_word(void)
{
    df_log_0("Data Flood For Easier PostgreSQL Test");
	df_log_0("Contact Author By mail 'lchch1990@sina.cn'");
}

void handle_argument(int argc, char *argv[])
{
    char c = 0;
    char* value = NULL;
    char* command = NULL;

    memset(&user_arg, 0, sizeof(UserArgument));

    while (-1 != (c = getopt_long(argc, argv, "hVP:U:p:d:c:r:j:n:u:m:vf",long_options, NULL)))
    {
        switch (c)
        {
            case 'h':
                do_help();
                break;
            case 'V':
                do_version();
                df_exit(NULL);
                break;
            case 'v':
                user_arg.verbose = true;
                break;
            case 'U':
                user_arg.db_user = (char*)strdup(optarg);
                break;
            case 'P':
                user_arg.db_password = (char*)strdup(optarg);
                break;
            case 'p':
                user_arg.db_port = (char*)strdup(optarg);
                break;
            case 'd':
                user_arg.db_name = (char*)strdup(optarg);
                break;
            case 'c':
                user_arg.config_path = (char*)strdup(optarg);
                break;
            case 'r':
                user_arg.rel_name = (char*)strdup(optarg);
                break;
            case 'u':
                user_arg.unique_column = (char*)strdup(optarg);
                break;
            case 'f':
                user_arg.force = true;
                break;
            case 'j':
                value = optarg;
                user_arg.thread_num = str_to_uint32(value);
                break;
            case 'n':
                value = optarg;
                user_arg.files = str_to_uint32(value);
                break;
            case 'm':
                value = optarg;
                if(value[0] == 'f')
                    user_arg.fastmode = true;
                else if(value[0] == 's')
                    user_arg.fastmode = false;
                else
                    df_error("Wrong mode %c", value[0]);
                break;
            default:
                df_error("Unsupported argument %c", c);
                break;
        }
    }

    if(!user_arg.rel_name && !user_arg.config_path)
        df_error("No relname(-r) or configure file(-c) for watering data");
    if(user_arg.files == 0)
        df_error("No file number(-n) for watering data");

    if(user_arg.thread_num == 0)
        user_arg.thread_num = 1;
    if(user_arg.config_path)
        read_table_def(user_arg.config_path);
}

void main(int argc, char* argv[])
{
    uint64 start_time = 0;
    uint64 end_time = 0;
    handle_argument(argc, argv);

    doConnect(NULL);
    table_watering(user_arg.rel_name, user_arg.files);
    show_table_size(user_arg.rel_name);
    doDisconnect();
}